<?

/*	MediaChart OBJECT
*	Methods:
*	output
*
*
*					*/


class MediaChart extends modTEXT {

	/**
	 * Define Google Chart API: https url 
	 *
	 * @name api	defines API url 
	 */
	const api	= "https://chart.googleapis.com/";
	
	
	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
	}
	
	
	public function construct() {
				
	}
	
	
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		if( $result )
			$this->addclass( $this->action );
			
		if( is_array( $result ))
			$result = "<ul>" . implode( "\n", $result ) . "</ul>";
			
		if( $result )
			return $this->parse( $result ) . $this->render_js();
		
	}
	
	
	function filter_simple() {
		
		if( $this->vars->color )	$addt[ "chco"] = $this->vars->color;
		if( $this->vars->legend )	$addt[ "chdl"] = $this->vars->legend;
		if( $this->vars->labal )	$addt[ "chl" ] = $this->vars->labal;
		
		
		$result = $this->render_chart( $this->vars->type, $this->vars->data, $this->vars->size, $addt );
		
		return $result;
		
	}
	
	function filter_average() {
		
		if( $this->vars->color )	$addt[ "chco"] = $this->vars->color;
		
		if( $this->vars->label )	$addt[ "chl" ] = $this->vars->label;
		
		foreach( $this->vars->data as $part )
			$raw[ $part->key ] = $raw[ $part->key ] + (int) ( $part->prc )? $part->n * $part->prc * 0.01 : $part->n;
			
		foreach( $raw as $key => $val ) {
			$data[ $key ] = round( $val / array_sum( $raw ) * 100, 2 );
			$keys[] = $key . " (" . round( $data[ $key ] ) . ")";
		}
		
		
		$addt[ "chdl"] = implode( "|", $keys );
		
		$result = $this->render_chart( $this->vars->type, "t:" . implode( ",", $data ), $this->vars->size, $addt );
		
		return $result;
		
	}
	
	
	
	
	function ajax_chart() {
		
		return $this->filter_chart();
	}
	
	
	private function render_chart( $type, $data, $size, $condt = false ) {
		
		if( $condt )
			foreach( $condt as $key => $val ) $add .= "&amp;$key=$val"; 
		
		if( $type )
			
			return "<img src='". self::api ."chart?cht=$type&amp;chd=$data&amp;chs=$size$add'/>";
	}

} 

?>